package com.ergwang.service.admin;

import com.ergwang.Model.Admin;
import com.ergwang.Model.admin.bo.*;
import com.ergwang.dao.AdminDao;
import com.ergwang.dao.AdminDaoImpl;

import java.util.List;

/**
 * Creat by ergWang
 * Date: 2020/5/29
 * Time: 00:16
 */
public class AdminServiceImpl implements AdminService{
    private AdminDao adminDao = new AdminDaoImpl();


    public Admin login(AdminLoginBO loginBO) {
        Admin admin = new Admin();
        admin.setEmail(loginBO.getEmail());
        admin.setPwd(loginBO.getPwd());
        return adminDao.login(admin);
    }

    public List<Admin> allAdmins() {
        return adminDao.allAdmins();
    }

    public List<Admin> getSearchAdmins(AdminSearchBO searchBO) {
        Admin admin = new Admin();
        admin.setEmail(searchBO.getEamil());
        admin.setNickname(searchBO.getNickname());
        return adminDao.getSearchAdmins(admin);
    }

    public int addAdminss(AdminAddBO addBO) {
        Admin admin = new Admin();
        admin.setEmail(addBO.getEmail());
        admin.setNickname(addBO.getNickname());
        admin.setPwd(addBO.getPwd());
        return adminDao.addAdminss(admin);
    }

    public int deleteAdmins(int id) {
        Admin admin = new Admin();
        admin.setId(id);
        return adminDao.deleteAdmins(admin);
    }

    public Admin getAdminsInfo(Admin admin) {
        return adminDao.getAdminsInfo(admin);
    }

    public int updateAdminss(AdminUpdateBO updateBO) {
        Admin admin = new Admin();
        admin.setId(updateBO.getId());
        admin.setEmail(updateBO.getEmail());
        admin.setNickname(updateBO.getNickname());
        admin.setPwd(updateBO.getPwd());
        return adminDao.updateAdminss(admin);
    }

    /**
     * 修改密码
     * 成功返回1，旧密码错返回404，两次不一致500
     * @param changePwdBO
     * @return
     */
    public int changePwd(AdminChangePwdBO changePwdBO) {
        // 判断密码是否正确
        String oldPwd = adminDao.getOldPwd(changePwdBO.getAdminToken());
        if (!oldPwd.equals(changePwdBO.getOldPwd())){
            return 404;
        }
        // 判断两次密码是否一致
        if (!changePwdBO.getNewPwd().equals(changePwdBO.getConfirmPwd())){
            return 500;
        }
        // 修改密码
        return adminDao.changePwd(changePwdBO);
    }

}
